package com.wang.mvchain.outer.service.implay;

import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.apache.commons.codec.digest.HmacUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wang.mvchain.common.util.ConstatFinalUtil;
import com.wang.mvchain.common.util.HTTPUtil;
import com.wang.mvchain.outer.enums.TradeEnum;
import com.wang.mvchain.outer.service.ITradeService;

/**
 * 币安的API工具
 * 
 * @author wangsh
 */
public class BinanceTradeServiceImplay extends BaseTradeService implements ITradeService
{
	/* 请求服务的serverUrl */
	private String serverUrl = "https://www.binance-cn.com" ;
	private String recvWindow = "10000"; 
	
	@Override
	public int findWebsiteId()
	{
		return 18;
	}
	
	/**
	 * 获取签名方式
	 * @param json 是一个JSON字符串
	 * @return
	 */
	private String authSign(Map<String, String> headerMap,Map<String, String> requestMap)
	{
		StringBuffer sb = new StringBuffer();
		/* 拼装请求参数为一个字符串 */
		for (Iterator iterator = requestMap.entrySet().iterator(); iterator.hasNext();)
		{
			Entry me = (Entry) iterator.next();
			String key = me.getKey() + "" ;
			String value = me.getValue() + ""; 
			if("requestURL".equalsIgnoreCase(key))
			{
				continue ; 
			}else
			{
				sb.append(key + "=" + value + "&");
			}
		}
		if(sb.toString().endsWith("&"))
		{
			sb.delete(sb.lastIndexOf("&"), sb.length());
		}
		//ConstatFinalUtil.SYS_LOG.info("加密前:{}",sb.toString());
		/*
		 * 采用HMacSha256加密
		 * */
		String resStr = sb.toString();
		String secretKey = authJSON.getString("secretKey");
		//String secretKey = "NhqPtmdSJYdKjVHjA7PZj4Mge3R5YNiP1e3UZjInClVN65XAbvqqM6A7H5fATj0j";
		String res = HmacUtils.hmacSha256Hex(secretKey, resStr);
		//ConstatFinalUtil.SYS_LOG.info("加密后:{}",res);
		return res ; 
	}

	/**
	 * 获取用户基本信息
	 * 
	 * @return
	 */
	public JSONObject getUserInfo(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = new JSONObject();
		String methodUrl = "/api/v3/account" ; 
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		
		/* 存储参数 */
		requestMap.put("timestamp", this.findReqtime());
		requestMap.put("recvWindow", recvWindow );
		
		/**
		 * 存储Header的一些参数
		 */
		Map<String, String> headerMap = new HashMap<String, String>();
		headerMap.put("X-MBX-APIKEY", this.authJSON.getString("apiKey"));
		
		/* 存储一些交易的参数 */
		String sign = authSign(headerMap,requestMap);
		requestMap.put("signature", sign);
		
		String result = "" ; 
		try
		{
			result = httpUtil.methodGet(headerMap,requestMap);
			//ConstatFinalUtil.SYS_LOG.info("==服务器返回:{}==",result);
			JSONObject resJSON = (JSONObject) JSON.parse(result);
			/* 存储所有的余额信息 */
			JSONArray resArr = resJSON.getJSONArray("balances");
			
			JSONObject dataJSON = new JSONObject();
			JSONObject balanceJSON = new JSONObject();
			JSONObject fundJSON = new JSONObject();
			for (Iterator iterator = resArr.iterator(); iterator.hasNext();)
			{
				JSONObject resTempJSON = (JSONObject) iterator.next() ;
				/*
				 * {
				      "asset": "BTC",
				      "free": "4723846.89208129",
				      "locked": "0.00000000"
				    }
				 * */
				String name = resTempJSON.getString("asset").toLowerCase();
				/* 这里面有我们要想的币种 */
				balanceJSON.put(name, resTempJSON.get("free"));
				fundJSON.put(name, resTempJSON.get("locked"));
			}
			
			dataJSON.put("balance", balanceJSON);
			dataJSON.put("fund", fundJSON);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//ConstatFinalUtil.OUTER_LOG.info(this.findid() + "--获取用户信息返回.--" + responseJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "获取用户信息失败."
					+ requestMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	/**
	 * 下单交易
	 * 
	 * @return
	 */
	public JSONObject trade(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = super.trade(paramsMap);
		if(resultJSON != null)
		{
			return resultJSON ; 
		}
		
		resultJSON = new JSONObject() ;
		//拼装目标数据
		String methodUrl = "/api/v3/order" ; 
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		
		/**
		 * 存储Header的一些参数
		 */
		Map<String, String> headerMap = new HashMap<String, String>();
		
		/* 存储一些交易的参数 */
		String type = paramsMap.get("type") ; 
		if(TradeEnum.TRADE_TYPE_BUY.getValue() == Integer.valueOf(type))
		{
			/* 买 */
			requestMap.put("side", "BUY");
		}else if(TradeEnum.TRADE_TYPE_SELL.getValue() == Integer.valueOf(type))
		{
			/* 买 */
			requestMap.put("side", "SELL");
		}
		/* 交易对 */
		String moneyType = paramsMap.get("moneytype");
		moneyType = this.convertMoneyType(moneyType) ; 
		
		requestMap.put("symbol", moneyType);
		/* 订单类型 */
		requestMap.put("type", "LIMIT");
		/* timeInForce */
		requestMap.put("timeInForce", "GTC");
		/* 价格和数量 */
		requestMap.put("price", paramsMap.get("price"));
		requestMap.put("quantity", paramsMap.get("amount"));
		requestMap.put("timestamp", this.findReqtime());
		requestMap.put("recvWindow", recvWindow );
		
		String sign = authSign(headerMap,requestMap);
		requestMap.put("signature", sign);
		
		headerMap.put("X-MBX-APIKEY", this.authJSON.getString("apiKey"));
		
		String result = "" ; 
		try
		{
			result = httpUtil.methodPost(headerMap , requestMap);
			JSONObject responseJSON = (JSONObject) JSON.parse(result);
			
			if(responseJSON.get("orderId") != null )
			{
				JSONObject dataJSON = new JSONObject();
				dataJSON.put("order_id", responseJSON.get("orderId"));
				
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
				ConstatFinalUtil.OUTER_LOG.info(this.findWebsiteId() + "--下单交易返回--原始信息:" + result
					+ ";转换后目标信息:" + resultJSON);
			}else
			{
				resultJSON.put("code", "-6");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-6") + ";返回数据:" + responseJSON.get("error_code"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "下单交易失败."
					+ requestMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	/**
	 * 撤销订单
	 * 
	 * @return
	 */
	public JSONObject cancelOrder(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = super.cancelOrder(paramsMap);
		if(resultJSON != null)
		{
			return resultJSON ; 
		}
		//拼装目标数据
		resultJSON = new JSONObject();
		String methodUrl = "/api/v3/order"; 
		
		String orderId = paramsMap.get("order_id");
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		/**
		 * 存储Header的一些参数
		 */
		Map<String, String> headerMap = new HashMap<String, String>();
		
		/* 存储一些交易的参数 */
		String moneyType = paramsMap.get("moneytype") ; 
		moneyType = this.convertMoneyType(moneyType) ; 
		requestMap.put("symbol", moneyType);
		requestMap.put("orderId", orderId);
		
		requestMap.put("timestamp", this.findReqtime());
		requestMap.put("recvWindow", recvWindow );
		
		String sign = authSign(headerMap,requestMap);
		requestMap.put("signature", sign);
		
		headerMap.put("X-MBX-APIKEY", this.authJSON.getString("apiKey"));
		
		String result = "" ; 
		try
		{
			result = httpUtil.methodDelete(headerMap,requestMap);
			JSONObject responseJSON = (JSONObject) JSON.parse(result);
			
			if(responseJSON.get("orderId") != null)
			{
				JSONObject dataJSON = new JSONObject();
				dataJSON.put("order_id", responseJSON.get("orderId"));
				
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
				ConstatFinalUtil.OUTER_LOG.info(this.findWebsiteId() + "--撤销订单返回.--原始信息:" + result
						+ ";转换后目标信息:" + resultJSON);
			}else
			{
				resultJSON.put("code", "-6");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-6") + ";返回数据:" + responseJSON.get("error_code"));
			}
			
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "撤销订单失败."
					+ paramsMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	/**
	 * 获取用户挂单
	 * 
	 * @param args
	 */
	public JSONObject getOrders(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = super.getOrders(paramsMap);
		if(resultJSON != null)
		{
			return resultJSON ; 
		}
		resultJSON = new JSONObject() ;
		//拼装目标数据
		String methodUrl = "/api/v3/openOrders" ; 
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		
		/**
		 * 存储Header的一些参数
		 */
		Map<String, String> headerMap = new HashMap<String, String>();
		
		/* 存储一些交易的参数 */
		String moneyType = paramsMap.get("moneytype") ; 
		requestMap.put("symbol", this.convertMoneyType(moneyType));
		
		requestMap.put("timestamp", this.findReqtime());
		requestMap.put("recvWindow", recvWindow );
		
		headerMap.put("X-MBX-APIKEY", this.authJSON.getString("apiKey"));
		
		/* 存储一些交易的参数 */
		String sign = authSign(headerMap,requestMap);
		requestMap.put("signature", sign);
				
		String result = "" ; 
		try
		{
			result = httpUtil.methodGet(headerMap , requestMap);
			JSONArray ordersJSON = (JSONArray) JSON.parse(result);
			
			JSONObject dataJSON = new JSONObject();
			JSONArray ordersResArr = new JSONArray();
			for (Iterator iterator = ordersJSON.iterator(); iterator
					.hasNext();)
			{
				JSONObject ordersTemp = (JSONObject) iterator.next();
				JSONObject ordersResJSON = this.convertOrders(moneyType, ordersTemp);
				ordersResArr.add(ordersResJSON);
			}
			
			dataJSON.put("ordersList", ordersResArr);
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			ConstatFinalUtil.OUTER_LOG.info(this.findWebsiteId() + "--获取用户挂单--原始信息:" + result
				+ ";转换后目标信息:" + resultJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "获取用户挂单失败."
					+ paramsMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	/**
	 * 行情API
	 */
	public JSONObject queryTicker(Map<String, String> paramsMap)
	{

		HTTPUtil httpUtil = new HTTPUtil();
		String symbol = paramsMap.get("symbol");
		if ("ltc_cny".equalsIgnoreCase(symbol))
		{
			paramsMap.put("requestURL",
					"https://www.okcoin.com/api/ticker.do?symbol=ltc_cny");
		} else
		{
			paramsMap.put("requestURL", "https://www.okcoin.com/api/ticker.do");
		}

		paramsMap.remove("symbol");

		JSONObject resultJSON = new JSONObject();
		try
		{
			String result = httpUtil.methodGet(paramsMap);
			resultJSON = (JSONObject) JSON.parse(result);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "行情API失败."
					+ paramsMap, e);
		}
		return resultJSON;
	}

	/**
	 * depth(市场深度)
	 */
	public JSONObject queryDepth(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = new JSONObject();
		String moneyType = paramsMap.get("moneytype");
		moneyType = this.convertMoneyType(moneyType);
		
		String methodUrl = "/api/v1/depth?symbol=" + moneyType ; 
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		/* 增加签名参数 */
		/* 存储一些交易的参数 */
		JSONObject responseJSON = new JSONObject();
		try
		{
			String result = httpUtil.methodGet(requestMap);
			responseJSON = (JSONObject) JSON.parse(result);
			
			// 拼装符合条件的json数据
			JSONObject dataJSON = new JSONObject();

			int count = 0;
			// 按照规则排序的容器
			Map<Double, JSONObject> asksMap = new TreeMap<Double, JSONObject>();
			JSONArray asksResArr = new JSONArray();
			JSONArray asksArr = (JSONArray) responseJSON.get("asks");
			for (Iterator iterator = asksArr.iterator(); iterator.hasNext();)
			{
				JSONArray asksTempArr = (JSONArray) iterator.next();
				JSONObject askJSON = new JSONObject();
				askJSON.put("count", "1");
				askJSON.put("vol", asksTempArr.get(1) + "");
				askJSON.put("rate", asksTempArr.get(0) + "");

				asksMap.put(Double.valueOf(askJSON.get("rate") + ""), askJSON);
			}

			for (Iterator iterator = asksMap.entrySet().iterator(); iterator
					.hasNext();)
			{
				Entry me = (Entry) iterator.next();
				double key = (Double) me.getKey();

				if (count >= ConstatFinalUtil.PAGE_BATCH_SIZE)
				{
					break;
				}

				JSONObject asksTemp = (JSONObject) me.getValue();

				asksResArr.add(asksTemp);
				count++;
			}

			dataJSON.put("asks", asksResArr);

			// 按照规则排序的容器
			count = 0;
			Map<Double, JSONObject> bidsMap = new TreeMap<Double, JSONObject>(
					new Comparator<Double>()
					{
						@Override
						public int compare(Double o1, Double o2)
						{
							return o2.compareTo(o1);
						}
					});
			JSONArray bidsResArr = new JSONArray();
			JSONArray bidsArr = (JSONArray) responseJSON.get("bids");
			for (Iterator iterator = bidsArr.iterator(); iterator.hasNext();)
			{
				JSONArray bidsTempArr = (JSONArray) iterator.next();
				JSONObject bidsJSON = new JSONObject();
				bidsJSON.put("count", "1");
				bidsJSON.put("vol", bidsTempArr.get(1) + "");
				bidsJSON.put("rate", bidsTempArr.get(0) + "");

				bidsMap.put(Double.valueOf(bidsJSON.get("rate") + ""), bidsJSON);
			}

			for (Iterator iterator = bidsMap.entrySet().iterator(); iterator
					.hasNext();)
			{
				Entry me = (Entry) iterator.next();
				double key = (Double) me.getKey();

				if (count >= ConstatFinalUtil.PAGE_BATCH_SIZE)
				{
					break;
				}

				JSONObject bidsTemp = (JSONObject) me.getValue();

				bidsResArr.add(bidsTemp);
				count++;
			}
			dataJSON.put("bids", bidsResArr);

			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			
			resultJSON.put("data", dataJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "depth(市场深度)失败."
					+ paramsMap, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	/**
	 * trades的格式,历史交易记录
	 */
	public JSONArray queryHistoryTrades(String symbol,
			Map<String, String> paramsMap)
	{

		HTTPUtil httpUtil = new HTTPUtil();

		if ("ltc_cny".equalsIgnoreCase(symbol))
		{
			paramsMap.put("requestURL",
					" https://www.okcoin.com/api/trades.do?symbol=ltc_cny");
		} else
		{
			paramsMap.put("requestURL",
					" https://www.okcoin.com/api/trades.do?");
		}
		JSONArray resultJSON = new JSONArray();
		try
		{
			String result = httpUtil.methodGet(paramsMap);
			resultJSON = (JSONArray) JSON.parse(result);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "历史交易记录失败."
					+ paramsMap, e);
		}
		return resultJSON;
	}

	@Override
	public String toString()
	{
		return "OkexTradeServiceImplay [findid()=" + findWebsiteId() + "]";
	}

	@Override
	public JSONObject getOrdersSingle(Map<String, String> paramsMap)
	{
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		JSONObject resultJSON = super.getOrdersSingle(paramsMap);
		if(resultJSON != null)
		{
			return resultJSON ; 
		}
		
		resultJSON = new JSONObject();
		//拼装目标数据
		String methodUrl = "/api/v3/order"; 
		
		String orderId = paramsMap.get("order_id");
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		
		/**
		 * 存储Header的一些参数
		 */
		Map<String, String> headerMap = new HashMap<String, String>();
		
		/* 存储一些交易的参数 */
		String moneyType = paramsMap.get("moneytype") ; 
		requestMap.put("symbol", this.convertMoneyType(moneyType));
		requestMap.put("orderId", orderId);
		
		requestMap.put("timestamp", this.findReqtime());
		requestMap.put("recvWindow", recvWindow );
		
		String sign = authSign(headerMap,requestMap);
		requestMap.put("signature", sign);
		
		headerMap.put("X-MBX-APIKEY", this.authJSON.getString("apiKey"));
		
		String result = "" ; 
		try
		{
			result = httpUtil.methodGet(headerMap , requestMap);
			JSONObject ordersTemp = (JSONObject) JSON.parse(result);
			
			JSONObject dataJSON = new JSONObject() ; 
			if(ordersTemp.get("orderId") != null)
			{
				JSONObject ordersResJSON = this.convertOrders(moneyType, ordersTemp);
				dataJSON.put("orders", ordersResJSON);
				resultJSON.put("data", dataJSON);
				resultJSON.put("code", "0");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
				ConstatFinalUtil.OUTER_LOG.info(this.findWebsiteId() + "--获取订单详情--原始信息:" + result
						+ ";转换后目标信息:" + resultJSON);
			}else
			{
				resultJSON.put("code", "-6");
				resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-6") + ";返回数据:" + ordersTemp.get("error_code"));
			}
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "获取订单详情失败."
					+ paramsMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}

	@Override
	public String findWebSiteName()
	{
		return "Binance";
	}

	@Override
	public JSONObject convertOrders(String moneytype, JSONObject ordersTemp)
	{
		Calendar now = Calendar.getInstance() ; 
		try
		{
			JSONObject ordersResJSON = new JSONObject();
			ordersResJSON.put("order_id", ordersTemp.get("orderId") + "");
			ordersResJSON.put("price", ordersTemp.get("price") + "");
			ordersResJSON.put("amount", ordersTemp.get("origQty") + "");
			ordersResJSON.put("deal_amount", ordersTemp.get("executedQty") + "");
			/* avg_price:实际成交的价格 */
			ordersResJSON.put("avg_price", ordersResJSON.get("price"));
			/* 创建订单的时间 */
			String createTime = ordersTemp.get("time") + "";
			now.setTimeInMillis(Long.valueOf(createTime));
			Date createDate = now.getTime();
			ordersResJSON.put("create_date", this.dateUtil.formatDateTime(createDate));
			
			String symbol = ordersTemp.get("symbol") + "";
			ordersResJSON.put("symbol", this.parseMoneyType(symbol));
			/* 交易类型 */
			String type = ordersTemp.get("side") + "";
			if("buy".equalsIgnoreCase(type))
			{
				type = TradeEnum.TRADE_TYPE_BUY.getValue() + "" ; 
			}else if("sell".equalsIgnoreCase(type))
			{
				type = TradeEnum.TRADE_TYPE_SELL.getValue() + "" ; 
			}
			ordersResJSON.put("type", type);
			
			/* 默认未成交 */
			String status = TradeEnum.TRADE_STATUS_DEAL_NO.getValue() + ""; 
			/*
			 * -1:已撤销  0:未成交  1:部分成交  2:完全成交 3 撤单处理中
			 * */
			if("CANCELED".equalsIgnoreCase(ordersTemp.get("status") + ""))
			{
				status = TradeEnum.TRADE_STATUS_CANCEL.getValue() + ""; 
			}else if("NEW".equalsIgnoreCase(ordersTemp.get("status") + ""))
			{
				status = TradeEnum.TRADE_STATUS_DEAL_NO.getValue() + ""; 
			}else if("PARTIALLY_FILLED".equalsIgnoreCase(ordersTemp.get("status") + ""))
			{
				status = TradeEnum.TRADE_STATUS_DEAL_PART.getValue() + ""; 
			}else if("FILLED".equalsIgnoreCase(ordersTemp.get("status") + ""))
			{
				status = TradeEnum.TRADE_STATUS_DEALED.getValue() + ""; 
			}
			ordersResJSON.put("status", status);
			return ordersResJSON;
		} catch (Exception e)
		{
			ConstatFinalUtil.SYS_LOG.error("转换订单报错了",e);
		}
		return new JSONObject();
	}

	@Override
	public String convertMoneyType(String moneytypeSelf)
	{
		return moneytypeSelf.replace("_", "").toUpperCase();
	}

	@Override
	public String parseMoneyType(String moneytypeTrade)
	{
		String indexStr = "usdt" ; 
		moneytypeTrade = moneytypeTrade.toLowerCase() ; 
		/* 必须是美金操作 */
		int usdtIndex = moneytypeTrade.indexOf(indexStr);
		String moneySou = moneytypeTrade.substring(0, usdtIndex);
		String moneyTar = moneytypeTrade.substring(usdtIndex, moneytypeTrade.length());
		return moneySou + "_" + moneyTar;
	}
	
	@Override
	public JSONObject findAllTransPair(Map<String, String> paramsMap)
	{
		JSONObject resultJSON =  super.findAllTransPair(paramsMap);
		long st = System.currentTimeMillis() ; 
		//拼装目标数据
		String methodUrl = "/api/v1/exchangeInfo" ; 
		
		HTTPUtil httpUtil = new HTTPUtil();
		/* 公共代码 */
		Map<String, String> requestMap = new TreeMap<String, String>();
		requestMap.put("requestURL", serverUrl + methodUrl);
		
		String result = "" ; 
		try
		{
			result = httpUtil.methodGet(Collections.EMPTY_MAP,requestMap);
			JSONObject resJSON = (JSONObject) JSON.parse(result);
			
			JSONArray dataResArr = resJSON.getJSONArray("symbols");
			JSONObject dataJSON = new JSONObject();
			for (Iterator iterator = dataResArr.iterator(); iterator.hasNext();)
			{
				JSONObject resTempJSON = (JSONObject) iterator.next() ;
				/*
				 * {
				        //交易货币币种,小写;
				        "base_currency":"BTC",
				        //币对名称,小写;
				        "instrument_id":"BTC-USDT",
				        //最小交易数量
				        "min_size":"0.001",
				        //计价货币币种,小写;
				        "quote_currency":"USDT",
				        //交易货币数量精度
				        "size_increment":"0.00000001",
				        //交易价格精度
				        "tick_size":"0.1"
				    }
				 * */
				JSONObject pairJSON = new JSONObject();
				String moneyType = resTempJSON.getString("symbol");
				
				if(!moneyType.toLowerCase().endsWith("usdt"))
				{
					continue ; 
				}
				
				moneyType = this.parseMoneyType(moneyType);
				pairJSON.put("name", moneyType);
				pairJSON.put("base_currency", resTempJSON.getString("baseAsset").toLowerCase());
				pairJSON.put("quote_currency", resTempJSON.getString("quoteAsset").toLowerCase());
				pairJSON.put("size_increment", resTempJSON.get("baseAssetPrecision"));
				pairJSON.put("tick_size", resTempJSON.get("quotePrecision"));
				/* 获取过滤器 */
				JSONArray filterArr = resTempJSON.getJSONArray("filters");
				for (Iterator iterator2 = filterArr.iterator(); iterator2.hasNext();)
				{
					JSONObject filterTemp = (JSONObject) iterator2.next();
					String filterType = filterTemp.getString("filterType");
					if("LOT_SIZE".equalsIgnoreCase(filterType))
					{
						pairJSON.put("min_size", filterTemp.get("minQty"));
					}
				}
				
				/* 存储到结果中 */
				dataJSON.put(pairJSON.get("name") + "", pairJSON);
			}
			
			resultJSON.put("data", dataJSON);
			resultJSON.put("code", "0");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("0"));
			//ConstatFinalUtil.OUTER_LOG.info(this.findid() + "--获取用户信息返回.--" + responseJSON);
		} catch (Exception e)
		{
			ConstatFinalUtil.OUTER_LOG.error(this.findWebsiteId() + "获取findAllTransPair失败."
					+ requestMap + ";原始信息:" + result, e);
			resultJSON.put("code", "-1");
			resultJSON.put("info", ConstatFinalUtil.INFO_JSON.get("-1"));
		}
		long ed = System.currentTimeMillis() ;
		resultJSON.put("consume", (ed -st) + "毫秒");
		return resultJSON;
	}
}
